#include <mutex>
#include <shared_mutex>
#include <iostream>
#include <thread>
#include <iostream>
#include <string>
using namespace std;

shared_mutex g_mutex;
std::string g_str;

void readLoop()
{
    while ( true ) {
        this_thread::sleep_for( chrono::milliseconds( 100 ) );
        g_mutex.lock_shared();
        cout << g_str;
        g_mutex.unlock_shared();
    }
}

void writeLoop()
{
    int number = 0;
    while ( true ) {
        this_thread::sleep_for( chrono::milliseconds( 100 ) );
        g_mutex.lock();
        g_str = to_string( number++ ) + "\n";
        g_mutex.unlock();
    }
}

int main()
{
    thread( writeLoop ).detach();
    thread( readLoop ).detach();
    thread( readLoop ).detach();
    system( "pause" );
}